package com.cq.androidtools.jetpack2

import android.util.Log
import androidx.recyclerview.widget.RecyclerView
import androidx.work.*
import com.cq.androidtools.databinding.AcWorker2Binding
import com.cq.androidtools.jetpack.databinding.DataBindingActivity2

/**
 * 作者: CQ
 * 日期: 2022-02-10
 * 说明:
 */
class WorkActivity2 : DataBindingActivity2<AcWorker2Binding>() {
    override fun intData() {

        mBinding.OneTimeWorkRequestBuilder.setOnClickListener {
            // 传递给Worker的参数
            val data = Data.Builder().putString("a", "a 1").build()

            // Worker执行的约束条件
            val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
val recyclerView : RecyclerView? = null
            // 创建WorkRequest
            val oneTimeWorkRequest = OneTimeWorkRequestBuilder<UploadWorker>()
                .addTag("UploadWorker")
                .setInputData(data)
                .setConstraints(constraints)
                .build()
            WorkManager.getInstance(this).enqueue(oneTimeWorkRequest)
            WorkManager.getInstance(this).getWorkInfoByIdLiveData(oneTimeWorkRequest.id)
                .observe(this, {
                    if (it != null && it.state.isFinished) {
                        //在任务执行完成后
                        Log.i("TAG", "activity取到了回传的数据--- " + it.outputData.getString("name"));
                    }
                })
        }
    }

}